package com.py.korona.db;

import java.util.List;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Pair;

public class UriBuilder {
    public static String contentType(String uriBasePath) {
        return ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + uriBasePath;
    }

    public static Uri objectFilterUri(String uriAuthority, String uriBasePath, ContentValues values) {
        return objectFilterUriWithCustomPath("filter", uriAuthority, uriBasePath, values);
    }

    public static Uri objectFilterUriWithCustomPath(String customPath, String uriAuthority, String uriBasePath, ContentValues values) {
        Uri.Builder builder = new Uri.Builder();
        builder.scheme("content");
        builder.authority(uriAuthority);
        StringBuilder sb = new StringBuilder();
        sb.append(uriBasePath);
        sb.append("/");
        if (!TextUtils.isEmpty(customPath)) {
            sb.append(customPath);
        }
        builder.path(sb.toString());
        /*
         * apply params from ContentValues
         */
        if (null != values) {
            for (String paramName : values.keySet()) {
                String paramValue = values.getAsString(paramName);
                builder.appendQueryParameter(paramName, paramValue);
            }
        }
        return builder.build();
    }

    public static Uri objectListUri(String uriAuthority, String uriBasePath) {
        Uri.Builder builder = new Uri.Builder();
        builder.scheme("content");
        builder.authority(uriAuthority);
        builder.path(uriBasePath);
        return builder.build();
    }

    public static Uri objectListUri(String uriAuthority, String uriBasePath, List<Pair<String, List<String>>> list) {
        return objectListUriWithCustomPath("list", uriAuthority, uriBasePath, list);
    }

    public static Uri objectListUriWithCustomPath(String customPath, String uriAuthority, String uriBasePath) {
        Uri.Builder builder = new Uri.Builder();
        builder.scheme("content");
        builder.authority(uriAuthority);
        StringBuilder sb = new StringBuilder();
        sb.append(uriBasePath);
        sb.append("/");
        if (!TextUtils.isEmpty(customPath)) {
            sb.append(customPath);
        }
        builder.path(sb.toString());
        return builder.build();
    }

    public static Uri objectListUriWithCustomPath(String customPath, String uriAuthority, String uriBasePath, List<Pair<String, List<String>>> list) {
        Uri.Builder builder = new Uri.Builder();
        builder.scheme("content");
        builder.authority(uriAuthority);
        StringBuilder sb = new StringBuilder();
        sb.append(uriBasePath);
        sb.append("/");
        if (!TextUtils.isEmpty(customPath)) {
            sb.append(customPath);
        }
        builder.path(sb.toString());
        for (Pair<String, List<String>> entry : list) {
            StringBuilder sbPair = new StringBuilder();
            boolean hasComma = false;
            for (String value : entry.second) {
                sbPair.append((hasComma ? "," : "") + value);
                hasComma = true;
            }
            builder.appendQueryParameter(entry.first, sbPair.toString());
        }
        return builder.build();
    }
}
